package com.lecode.LRU.lru2;

/**
 * 双向链表
 */
public class LinkNode {
    /**
     * 头结点、尾结点
     */
    private Node head, tail;

    private Integer size;

    public LinkNode(){
        this.head = new Node(0, 0);
        this.tail = new Node(0, 0);
        head.next = tail;
        tail.prev = head;
        this.size = 0;
    }

    /**
     * 尾插法
     */
    public void addFromEnd(Node node){
        node.next = tail;
        node.prev = tail.prev;
        tail.prev.next = node;
        tail.prev = node;
        size++;
    }

    /**
     * 移除
     * @param node
     * @return
     */
    public void remove(Node node){
        node.prev.next = node.next;
        node.next.prev = node.prev;
        size--;
    }

    /**
     * 移除头结点
     * @return
     */
    public Node removeFirst(){
        if (this.head.next == this.tail) return null;
        Node first = head.next;
        remove(first);
        return first;
    }
}
